package com.witte.controller;



import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.reflect.TypeToken;
import com.witte.entity.City;
import com.witte.entity.RespBean;
import com.witte.otherApi.WeatherManager;
import com.witte.utils.FileUtil;
import com.witte.utils.GsonUtils;



@RestController
@ResponseBody
@RequestMapping("/")
@CrossOrigin
public class WeatherAPIContoller {

    @Autowired
    WeatherManager weatherManager;
    //用来校验传参是否正确
//    @Value("${city.code}")
//    private String cityCode;
    /**
     * 天气数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/city/{id:1[0-9]{8}}", method = RequestMethod.GET)
    public RespBean loadApi(@PathVariable("id") String id){
        String vliCode = String.format(",%s,", id);
//        if(!cityCode.contains(vliCode)){
//        	return RespBean.error("没有城市");
//        }
        return RespBean.ok("成功", weatherManager.getById(id));
    }
    
    @RequestMapping(value = "/citycode", method = RequestMethod.GET)
    public RespBean citycode() throws IOException {
    	String dir=System.getProperty("user.dir");
    	String json=FileUtil.readFileAsString(dir+File.separator+"static"+File.separator+"citycode.json");
    	List<City> list=GsonUtils.fromJson(json, new TypeToken<List<City>>() {}.getType());
    	return RespBean.ok("ok",list);
    }
}
